#ifndef BSP_WEEECOREBOT_H
#define BSP_WEEECOREBOT_H

#include "esp_err.h"
#include "driver/i2c_master.h"
#include <stdint.h>
#include <stdbool.h>

#ifdef __cplusplus
extern "C" {
#endif

// I2C通信配置
#define WEEECOREBOT_I2C_ADDRESS         0x28
#define WEEECOREBOT_I2C_TIMEOUT_MS      2000
#define WEEECOREBOT_MAX_DATA_LEN        48  // 匹配从机端I2C_TX_BUFFER_SIZE

// I2C指令定义
#define CMD_LED_CONTROL                0x10
#define CMD_MOTOR_CONTROL              0x20
#define CMD_DEVICE_INFO                0xAF
#define CMD_BATTERY_VOLTAGE            0x30
#define CMD_LINE_PATROL                0x40

// 响应状态码
#define RESPONSE_SUCCESS                0x01
#define RESPONSE_FAILED                 0xFE

// LED控制子指令
#define LED_CMD_TURN_ON                 0x01
#define LED_CMD_TURN_OFF                0x02
#define LED_CMD_DISABLE_BLINK           0x03
#define LED_CMD_ENABLE_BLINK            0x04

// 巡线传感器子指令
#define LINE_PATROL_CMD_SET_THRESHOLD  0x01
#define LINE_PATROL_CMD_READ_DATA      0x02

// 电机控制子指令
#define MOTOR_CMD_DIRECT_PWM           0x01
#define MOTOR_CMD_SMOOTH_PWM           0x02
#define MOTOR_CMD_SPEED_PID            0x03
#define MOTOR_CMD_POSITION_PID         0x04
#define MOTOR_CMD_STOP_ALL             0x05
#define MOTOR_CMD_READ_STATUS          0x06
#define MOTOR_CMD_RESET_POSITION       0x07
#define MOTOR_CMD_READ_RAW_DATA        0x08
#define MOTOR_CMD_SET_ANGLE_TARGET     0x0B
#define MOTOR_CMD_LINE_CONTROL         0x0C

// 数据结构定义
typedef struct {
    char device_name[16];
    char firmware_version[16];
    char build_date[16];
    char build_time[16];
} weeecorebot_device_info_t;

typedef struct {
    uint16_t sensor_data[4];  // 4个巡线传感器的ADC值
} weeecorebot_line_patrol_data_t;

typedef struct {
    int32_t left_position;    // 左电机位置
    int32_t right_position;   // 右电机位置
    float left_speed;         // 左电机滤波速度
    float right_speed;        // 右电机滤波速度
    float left_raw_speed;     // 左电机原始速度
    float right_raw_speed;    // 右电机原始速度
    int8_t left_direction;    // 左编码器方向
    int8_t right_direction;   // 右编码器方向
} weeecorebot_motor_status_t;

// 函数声明
// 添加Weeecore Bot设备到I2C总线
esp_err_t weeecorebot_add_device(i2c_master_bus_handle_t bus_handle, i2c_master_dev_handle_t *dev_handle);

// 读取设备信息
esp_err_t weeecorebot_read_device_info(i2c_master_dev_handle_t dev_handle, weeecorebot_device_info_t *info);

// LED控制
esp_err_t weeecorebot_led_control(i2c_master_dev_handle_t dev_handle, uint8_t led_cmd);

// 读取电池电压
esp_err_t weeecorebot_read_battery_voltage(i2c_master_dev_handle_t dev_handle, float *voltage);

// 设置巡线传感器阈值
esp_err_t weeecorebot_set_line_patrol_threshold(i2c_master_dev_handle_t dev_handle, uint16_t threshold);

// 读取巡线传感器数据
esp_err_t weeecorebot_read_line_patrol_data(i2c_master_dev_handle_t dev_handle, weeecorebot_line_patrol_data_t *data);

// 电机直接PWM控制
esp_err_t weeecorebot_motor_direct_pwm(i2c_master_dev_handle_t dev_handle, int8_t left_pwm, int8_t right_pwm);

// 电机平滑PWM控制
esp_err_t weeecorebot_motor_smooth_pwm(i2c_master_dev_handle_t dev_handle, int8_t left_pwm, int8_t right_pwm, uint8_t ramp_time);

// 电机速度PID控制
esp_err_t weeecorebot_motor_speed_pid(i2c_master_dev_handle_t dev_handle, int8_t left_rpm, int8_t right_rpm);

// 电机位置PID控制
esp_err_t weeecorebot_motor_position_pid(i2c_master_dev_handle_t dev_handle, int32_t left_position, int32_t right_position);

// 停止所有电机
esp_err_t weeecorebot_motor_stop_all(i2c_master_dev_handle_t dev_handle);

// 读取电机状态
esp_err_t weeecorebot_motor_read_status(i2c_master_dev_handle_t dev_handle, weeecorebot_motor_status_t *status);

// 重置电机位置
esp_err_t weeecorebot_motor_reset_position(i2c_master_dev_handle_t dev_handle);

// 设置电机角度目标
esp_err_t weeecorebot_motor_set_angle_target(i2c_master_dev_handle_t dev_handle, float angle_target);

// 控制电机直线行驶
esp_err_t weeecorebot_motor_line_control(i2c_master_dev_handle_t dev_handle, uint8_t direction);

#ifdef __cplusplus
}
#endif

#endif // BSP_WEEECOREBOT_H
